﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.AuthorizationManager.Data
{
	public class ResourceItemCollectionDictionary : Dictionary<string, ResourceItemCollection>
	{
		#region Public Members

		#region GetResourceItem

		public ResourceItem GetResourceItem(string resource, string action, string subAction, bool filterResourceActionItems)
		{
			var items = GetResourceItems(resource);
			if (items == null)
				return null;
			if (string.IsNullOrWhiteSpace(resource))
			{
				if (items.Count > 0)
					return items[0];
				return null;
			}
			else
			{
				return AuthorizationScopeItemManager.GetResourceItem(items, action, subAction, filterResourceActionItems);
			}
		}
		public ResourceItem GetResourceItem(string resource, string action, string subAction, string resourceItemId)
		{
			ResourceItemCollection items;

			if (string.IsNullOrWhiteSpace(resource))
				return null;
			if (string.IsNullOrWhiteSpace(resourceItemId))
				return null;
			resource = resource.Trim();
			resourceItemId = resourceItemId.Trim();
			if (TryGetValue(resource + AuthorizationScopeItemManager.ResourceItemSeparator + resourceItemId, out items))
				return AuthorizationScopeItemManager.GetResourceItem(items, action, subAction, false);
			return null;
		}

		#endregion
		#region GetResourceItems

		public ResourceItemCollection GetResourceItems(string resource)
		{
			ResourceItemCollection items;

			if (string.IsNullOrWhiteSpace(resource))
			{
				if (TryGetValue(string.Empty, out items))
					return items;
			}
			else
			{
				if (TryGetValue(resource.Trim(), out items))
					return items;
				if (TryGetValue(string.Empty, out items))
					return items;
			}
			return null;
		}
		public ResourceItemCollection GetResourceItems(string resource, string action)
		{
			var resourceItems = GetResourceItems(resource);
			if (resourceItems == null)
				return null;
			var items = new ResourceItemCollection();
			foreach (var resourceItem in resourceItems)
			{
				if (resourceItem.IsMatch(action))
					items.Add(resourceItem);
			}
			return items;
		}

		#endregion
		#region Load

		public void Load(IEnumerable<IAuthorizationScopeResourceAction> items)
		{
			ResourceItemCollectionDictionary index;
			ResourceItemCollection resourceItems;

			index = AuthorizationScopeItemManager.GetResourceItems(items);
			foreach (var indexItem in index)
			{
				resourceItems = new ResourceItemCollection();
				var orderedResourceItems = (from o in indexItem.Value
											orderby o.Priority
											select o);
				foreach (var orderedResourceItem in orderedResourceItems)
					resourceItems.Add(orderedResourceItem);
				Add(indexItem.Key, resourceItems);
			}
		}
		public void Load(IEnumerable<IAuthorizationScopeResourceItem> items)
		{
			ResourceItemCollectionDictionary index;
			ResourceItemCollection resourceItems;

			index = AuthorizationScopeItemManager.GetResourceItems(items);
			foreach (var indexItem in index)
			{
				resourceItems = new ResourceItemCollection();
				var orderedResourceItems = (from o in indexItem.Value
											orderby o.Priority
											select o);
				foreach (var orderedResourceItem in orderedResourceItems)
					resourceItems.Add(orderedResourceItem);
				Add(indexItem.Key, resourceItems);
			}
		}

		#endregion

		#endregion
		#region Constructors

		public ResourceItemCollectionDictionary()
			: base()
		{

		}
		public ResourceItemCollectionDictionary(IEqualityComparer<string> comparer)
			: base(comparer)
		{
		}

		#endregion
	}
}
